#!/bin/bash

npm run build

sftp_Host="114.67.105.74"
sftp_userName="root"
sftp_passWord="xlr@183721"
sftp_port=22
sftpRemotePath="/data/docker/pig/nginx/wwwroot"
sftpLocalPath="./dist"
current=$(date "+%Y-%m-%d %H:%M:%S")

echo "当前时间是：$current"

if [[ $# == 0 ]]; then
    yesterday=$(date "+%Y%m%d" -d "-1 days")
fi
if [[ $# == 1 ]]; then
    yesterday=$1
fi
myDir=$sftpLocalPath
if [[ ! -d $myDir ]]; then
    mkdir -p $myDir
fi

# SFTP非交互式操作

sftp_put()
{
    expect <<- EOF
    spawn sftp  -P $sftp_port $sftp_userName@$sftp_Host

    expect {
        "(yes/no)?" {send "yes\r"; expect_continue }
        "*assword:" {send "$sftp_passWord\r"}
    }
    expect "sftp>"
    set timeout -1
    send "put -r $sftpLocalPath $sftpRemotePath \r"
    expect "sftp>"
    send "bye\r"
EOF
}

# unGzipFiles(){
#     cd $myDir
#     fileList=`ls *`
#     fileArr=($fileList)
#     for fileName in ${fileArr[@]}
#     do
#         echo "开始解压文件：$fileName"
#         gzip -d  $fileName
#     done

# }

# reNameFiles(){
#     cd $myDir
#     fileList=`ls *`
#     fileArr=($fileList)

#     for fileName in ${fileArr[@]}
#     do
#         echo "reNameFile :$fileName"
#         mv $fileName $fileName".csv"
#     done
# }

echo "执行sftp上传操作 ： 数据日期："
sftp_put
echo "文件上传完成"
